
#include "plugin_manager.h"
#include "plugin_common.h"
#include <napi/native_api.h>

#include <ace/xcomponent/native_interface_xcomponent.h>
#include <stdint.h>
#include <string>
#include <stdio.h>

// 上下文枚举值
enum ContextType {
    APP_LIFECYCLE = 0,
    JS_PAGE_LIFECYCLE,
    XCOMPONENT_CONTEXT,
    XCOMPONENT_REGISTER_LIFECYCLE_CALLBACK,
    NATIVE_RENDER_API,
    WORKER_INIT,
    UV_ASYNC_SEND
};

PluginManager PluginManager::manager_;

// NAPI 接口

napi_value PluginManager::GetContext(napi_env env, napi_callback_info info) {
    napi_status status;
    napi_value exports;
    size_t argc = 1;
    napi_value args[1];
    napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);

    if (argc != 1) {
        napi_throw_type_error(env, NULL, "Wrong number of arguments");
        return nullptr;
    }

    napi_valuetype valuetype;
    status = napi_typeof(env, args[0], &valuetype);
    if (status != napi_ok) {
        return nullptr;
    }
    if (valuetype != napi_number) {
        napi_throw_type_error(env, NULL, "Wrong arguments");
        return nullptr;
    }

    int64_t value;
    napi_get_value_int64(env, args[0], &value);

    napi_create_object(env, &exports);

    switch (value) {
    case APP_LIFECYCLE: {
        /**** 注册生命周期函数  ******/
        napi_property_descriptor desc[] = {
            {"onCreate", nullptr, PluginManager::NapiOnCreate, nullptr, nullptr, nullptr, napi_default, nullptr},
            {"onShow", nullptr, PluginManager::NapiOnShow, nullptr, nullptr, nullptr, napi_default, nullptr},
            {"onHide", nullptr, PluginManager::NapiOnHide, nullptr, nullptr, nullptr, napi_default, nullptr},
            {"onDestroy", nullptr, PluginManager::NapiOnDestroy, nullptr, nullptr, nullptr, napi_default, nullptr}};
        napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
    }

    break;
    case JS_PAGE_LIFECYCLE: {
#ifdef _ARKUI_DECLARATIVE_
        /****************  声明式开发范式 JS Page 生命周期注册 ****************************/
        napi_property_descriptor desc[] = {
            {"onPageShow", nullptr, PluginManager::NapiOnPageShow, nullptr, nullptr, nullptr, napi_default, nullptr},
            {"onPageHide", nullptr, PluginManager::NapiOnPageHide, nullptr, nullptr, nullptr, napi_default, nullptr},
        };
        napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
#else
        /****************  类Web开发范式 JS Page 生命周期注册 ****************************/
        napi_property_descriptor desc[] = {
            {"onInit", nullptr, PluginManager::NapiOnInit, nullptr, nullptr, nullptr, napi_default, nullptr},
            {"onReady", nullptr, PluginManager::NapiOnReady, nullptr, nullptr, nullptr, napi_default, nullptr},
            {"onShow", nullptr, PluginManager::NapiOnShow, nullptr, nullptr, nullptr, napi_default, nullptr},
            {"onHide", nullptr, PluginManager::NapiOnHide, nullptr, nullptr, nullptr, napi_default, nullptr},
            {"onDestroy", nullptr, PluginManager::NapiOnDestroy, nullptr, nullptr, nullptr, napi_default, nullptr},
            {"onActive", nullptr, PluginManager::NapiOnActive, nullptr, nullptr, nullptr, napi_default, nullptr},
            {"onInactive", nullptr, PluginManager::NapiOnInactive, nullptr, nullptr, nullptr, napi_default, nullptr},
        };
        napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
#endif
    } break;
    case XCOMPONENT_REGISTER_LIFECYCLE_CALLBACK: {
        LOGE("Zcy cocos GetContext XCOMPONENT_REGISTER_LIFECYCLE_CALLBACK");
    } break;
    case NATIVE_RENDER_API:
        break;
    case WORKER_INIT: {
        LOGE("Zcy cocos worker: GetContext WORKER_INIT");
        napi_property_descriptor desc[] = {
            {"workerInit", nullptr, PluginManager::NapiWorkerInit, nullptr, nullptr, nullptr, napi_default, nullptr},
            //                    DECLARE_NAPI_FUNCTION("workerInit", PluginManager::NapiWorkerInit),
        };
        napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
    } break;
    case UV_ASYNC_SEND: {
        LOGE("Zcy cocos GetContext UV_ASYNC_SEND");
        napi_property_descriptor desc[] = {
            {"send", nullptr, PluginManager::NapiASend, nullptr, nullptr, nullptr, napi_default, nullptr},
            //                    DECLARE_NAPI_FUNCTION("send", PluginManager::NapiASend),
        };
        napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
    } break;
    default:
        LOGE("Zcy cocos unknown type");
    }
    return exports;
}

bool PluginManager::Export(napi_env env, napi_value exports) {
    LOGE("Zcy cocos PluginManager::Export");
    napi_status status;
    napi_value exportInstance = nullptr;
    OH_NativeXComponent *nativeXComponent = nullptr;
    int32_t ret;
    char idStr[OH_XCOMPONENT_ID_LEN_MAX + 1] = {};
    uint64_t idSize = OH_XCOMPONENT_ID_LEN_MAX + 1;

    status = napi_get_named_property(env, exports, OH_NATIVE_XCOMPONENT_OBJ, &exportInstance);
    if (status != napi_ok) {
        LOGE("napi_get_named_property error");
        return false;
    }

    status = napi_unwrap(env, exportInstance, reinterpret_cast<void **>(&nativeXComponent));
    if (status != napi_ok) {
        LOGE("napi_unwrap error");
        return false;
    }
    LOGE("Zcy cocos Export nativeXComponent = %{public}p", nativeXComponent);
    ret = OH_NativeXComponent_GetXComponentId(nativeXComponent, idStr, &idSize);
    if (ret != OH_NATIVEXCOMPONENT_RESULT_SUCCESS) {
        LOGE("OH_NativeXComponent_GetXComponentId error");
        return false;
    }

    std::string id(idStr);
    auto context = PluginManager::GetInstance();
    if (context) {
        context->SetNativeXComponent(id, nativeXComponent);
        auto render = context->GetRender(id);
        render->SetNativeXComponent(nativeXComponent);
        render->Export(env, exports);
        return true;
    }

    return false;
}

void PluginManager::SetNativeXComponent(std::string &id, OH_NativeXComponent *nativeXComponent) {
    if (nativeXComponentMap_.find(id) == nativeXComponentMap_.end()) {
        nativeXComponentMap_[id] = nativeXComponent;
    } else {
        if (nativeXComponentMap_[id] != nativeXComponent) {
            nativeXComponentMap_[id] = nativeXComponent;
        }
    }
}

OH_NativeXComponent *PluginManager::GetNativeXComponent(std::string &id) {
    if (nativeXComponentMap_.find(id) == nativeXComponentMap_.end()) {
        return nullptr;
    } else {
        return nativeXComponentMap_[id];
    }
}

PluginRender *PluginManager::GetRender(std::string &id) {
    if (pluginRenderMap_.find(id) == pluginRenderMap_.end()) {
        PluginRender *instance = PluginRender::GetInstance(id);
        pluginRenderMap_[id] = instance;
        return instance;
    } else {
        return pluginRenderMap_[id];
    }
}


napi_value PluginManager::NapiOnCreate(napi_env env, napi_callback_info info) {
    return nullptr;
}
napi_value PluginManager::NapiOnShow(napi_env env, napi_callback_info info) {
    PluginManager::GetInstance()->OnShowNative();
    return nullptr;
}
napi_value PluginManager::NapiOnHide(napi_env env, napi_callback_info info) {
    PluginManager::GetInstance()->OnHideNative();
    return nullptr;
}
napi_value PluginManager::NapiOnDestroy(napi_env env, napi_callback_info info) {
    PluginManager::GetInstance()->OnDestroyNative();
    return nullptr;
}

napi_value PluginManager::NapiWorkerInit(napi_env env, napi_callback_info info) {
    return nullptr;
}

napi_value PluginManager::NapiASend(napi_env env, napi_callback_info info) {
    LOGE("Zcy cocos PluginManager::NapiASend");
    return nullptr;
}

void PluginManager::OnShowNative() {
    LOGE("Zcy cocos PluginManager::OnShowNative");
}
void PluginManager::OnHideNative() {
    LOGE("Zcy cocos PluginManager::OnHideNative");
}
void PluginManager::OnDestroyNative() {
    LOGE("Zcy cocos PluginManager::OnDestroyNative");
}

#ifdef _ARKUI_DECLARATIVE_
napi_value PluginManager::NapiOnPageShow(napi_env env, napi_callback_info info) {
    LOGE("Zcy cocos PluginManager::NapiOnPageShow");
    return nullptr;
}

napi_value PluginManager::NapiOnPageHide(napi_env env, napi_callback_info info) {
    LOGE("Zcy cocos PluginManager::NapiOnPageHide");
    return nullptr;
}

void PluginManager::OnPageShowNative() {
    LOGE("Zcy cocos PluginManager::OnPageShowNative");
}

void PluginManager::OnPageHideNative() {
    LOGE("Zcy cocos PluginManager::OnPageHideNative");
}
#else

void PluginManager::OnInitNative() {
    LOGE("Zcy cocos PluginManager::OnInitNative");
    return;
}
void PluginManager::OnReadyNative() {
    LOGE("Zcy cocos PluginManager::OnReadyNative");
    return;
}
void PluginManager::OnShowNative() {
    LOGE("Zcy cocos PluginManager::OnShowNative");
    return;
}
void PluginManager::OnHideNative() {
    LOGE("Zcy cocos PluginManager::OnHideNative");
    return;
}
void PluginManager::OnDestroyNative() {
    LOGE("Zcy cocos PluginManager::OnDestroyNative");
    PluginManager::isPageInit = false;
    return;
}
void PluginManager::OnActiveNative() {
    LOGE("Zcy cocos PluginManager::OnActiveNative:");
    return;
}
void PluginManager::OnInactiveNative() {
    LOGE("Zcy cocos PluginManager::OnInactiveNative:");
    return;
}

napi_value PluginManager::NapiOnInit(napi_env env, napi_callback_info info) {
    PluginManager::GetInstance()->OnInitNative();
    return nullptr;
}
napi_value PluginManager::NapiOnReady(napi_env env, napi_callback_info info) {
    PluginManager::GetInstance()->OnReadyNative();
    return nullptr;
}
napi_value PluginManager::NapiOnShow(napi_env env, napi_callback_info info) {
    PluginManager::GetInstance()->OnShowNative();
    return nullptr;
}
napi_value PluginManager::NapiOnHide(napi_env env, napi_callback_info info) {
    PluginManager::GetInstance()->OnHideNative();
    return nullptr;
}
napi_value PluginManager::NapiOnDestroy(napi_env env, napi_callback_info info) {
    PluginManager::GetInstance()->OnDestroyNative();
    return nullptr;
}
napi_value PluginManager::NapiOnActive(napi_env env, napi_callback_info info) {
    PluginManager::GetInstance()->OnActiveNative();
    return nullptr;
}
napi_value PluginManager::NapiOnInactive(napi_env env, napi_callback_info info) {
    PluginManager::GetInstance()->OnInactiveNative();
    return nullptr;
}
#endif